﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Pirox_Profiler
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //Identifies whether you are logged in
        bool LoginStatus = false;

        //Password buffer
        string PasswordBuffer = Properties.Settings.Default.Password;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (Properties.Settings.Default.Firststart)
            {
                MessageBoxResult result = MessageBox.Show("Welcome to Pirox Profile Editor.\nDo you wish to change language to german?", "Welcome to Pirox Profile Editor", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    Properties.Settings.Default.Language = "Deutsch";
                    Properties.Settings.Default.Firststart = false;
                    Properties.Settings.Default.Save();
                }
                else
                {
                    Properties.Settings.Default.Firststart = false;
                    Properties.Settings.Default.Language = "English";
                    Properties.Settings.Default.Save();
                }
            }
            if (Properties.Settings.Default.TutorialDone == false)
            {
                switch (Properties.Settings.Default.Language)
                {
                    case "English":
                        MessageBox.Show("There is a tutorial. You may see it by clicking \"Help\".", "Tutorial", MessageBoxButton.OK);
                        break;
                    case "Deutsch":
                        MessageBox.Show("Es gibt ein Tutorial. Du kannst es ansehen, wenn du \"Hilfe\" klickst.", "Tutorial", MessageBoxButton.OK);
                        break;
                }

            }
            switch (Properties.Settings.Default.Language)
            {
                case "English":
                    break;
                case "Deutsch":
                    //Toolbar translation
                    File.Header = "Datei";
                    New.Header = "Neu";
                    Open.Header = "Öffnen";
                    Save.Header = "Speichern";
                    SaveAs.Header = "Speichern unter...";

                    Library.Header = "Bibliothek";
                    Login.Header = "Einloggen";
                    Download.Header = "Download";
                    Upload.Header = "Upload";
                    Profile.Header = "Nutzerprofil";
                    Logout.Header = "Ausloggen";

                    Settings.Header = "Einstellungen";
                    OpenSettings.Header = "Öffnen";
                    ResetSettings.Header = "Resetten";

                    Help.Header = "Hilfe";
                    Forums.Header = "Foren";
                    piroxbotscom.Header = "piroxbots.com";
                    Tutorial.Header = "Tutorial";

                    //Label translation
                    LastProjectLabel.Content = "Zuletzt geöffnete Projekte";
                    NotesLabel.Content = "Notizen";
                    ChosePartToRecordLabel.Content = "Wähle Teilprofil zum Aufnehmen";
                    WoWPathLabel.Content = "WoW Pfad";
                    pvpToolPathLabel.Content = "pvpTool Pfad";
                    AccountLabel.Content = "Account";
                    PasswordLabel.Content = "Passwort";
                    Opacitylabel.Content = "Deckkraft";

                    //Button translation
                    SaveButton.Content = "Speichern";
                    LoadButton.Content = "Laden";
                    NewButton.Content = "Neu";
                    ApplySettingsButton.Content = "Annehmen";
                    CancelSettingsButton.Content = "Abbrechen";
                    LoginButton.Content = "Einloggen";
                    LoginButton.ToolTip = "Hier klicken um einzuloggen. Du kannst Premiuminhalte nur eingeloggt nutzen.";
                    LoginCancelButton.Content = "Abbrechen";
                    LoginCancelButton.ToolTip = "Hier klicken um abzubrechen. Du kannst keine Premiuminhalte nutzen.";

                    //Tab translation
                    ProjecttabItem.Header = "Projekt";
                    CodeEditortabItem.Header = "Codeeditor";
                    DragAndDroptabItem.Header = "Drag&Drop";
                    RecordertabItem.Header = "Aufnehmen";
                    CustomClassEditortabItem.Header = "Benutzerklasseneditor";
                    LibrarytabItem.Header = "Bibliothek";

                    //Textbox translation
                    AccounttextBox.ToolTip = "Hier Accountnamen aus den Foren eingeben.";
                    PasswordtextBox.ToolTip = "Hier Passwort aus den Foren eingeben.";

                    //Checkbox translation
                    TopMostcheckBox.Content = "Immer anzeigen";
                    TopMostcheckBox2.Content = "Immer anzeigen";
                    RememberAccountcheckBox.Content = "Accountnamen merken";
                    RememberAccountcheckBox.ToolTip = "Aktivieren, um Accountnamen zu merken";
                    RememberPasswordcheckBox.Content = "Passwort merken";
                    RememberPasswordcheckBox.ToolTip = "Aktivieren, um Passwort zu merken";
                    break;
            }
            //Set window
            if (Properties.Settings.Default.Maximized)
            {
                this.WindowState = System.Windows.WindowState.Maximized;
            }
            else
            {
                this.Height = Properties.Settings.Default.Size.Height;
                this.Width = Properties.Settings.Default.Size.Width;
                this.WindowState = System.Windows.WindowState.Normal;
            }
            this.Topmost = Properties.Settings.Default.TopMost;

            TopMostcheckBox2.IsChecked = Properties.Settings.Default.TopMost;

            //Disable, 'cause not logged in yet
            Logout.IsEnabled = LoginStatus;
            Upload.IsEnabled = LoginStatus;
            Download.IsEnabled = LoginStatus;
            Profile.IsEnabled = LoginStatus;
        }

        private void ResetSettings_Click(object sender, RoutedEventArgs e)
        {
            switch (Properties.Settings.Default.Language)
            {
                case "English":
                    MessageBoxResult result = MessageBox.Show("Do you really want to reset all settings?", "Warning!", MessageBoxButton.YesNo);
                    if (result == MessageBoxResult.Yes)
                    {
                        Properties.Settings.Default.Reset();
                        Properties.Settings.Default.Save();
                        MessageBox.Show("Pirox Profile Editor will be closed now.", "Restart", MessageBoxButton.OK);
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("No changes.", "No changes", MessageBoxButton.OK);
                    }
                    break;
                case "Deutsch":
                    MessageBoxResult result2 = MessageBox.Show("Willst du wirklich alle Einstellungen zurücksetzen?", "Warnung!", MessageBoxButton.YesNo);
                    if (result2 == MessageBoxResult.Yes)
                    {
                        Properties.Settings.Default.Reset();
                        Properties.Settings.Default.Save();
                        MessageBox.Show("Pirox Profile Editor wird nun geschlossen.", "Neustart", MessageBoxButton.OK);
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("Es wurde nichts geändert.", "Keine Veränderung", MessageBoxButton.OK);
                    }
                    break;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Maximized)
            {
                Properties.Settings.Default.Maximized = true;
            }
            else
            {
                Properties.Settings.Default.Maximized = false;
                Properties.Settings.Default.Size = new System.Drawing.Size((int)this.Width, (int)this.Height);
            }
            Properties.Settings.Default.TopMost = this.Topmost;
            Properties.Settings.Default.Save();
        }

        private void OpenSettings_Click(object sender, RoutedEventArgs e)
        {
            EditorTabControl.Visibility = System.Windows.Visibility.Hidden;
            EditorTabControl.IsEnabled = false;
            SettingsGrid.Visibility = System.Windows.Visibility.Visible;
            SettingsGrid.IsEnabled = true;
            LoginGrid.Visibility = System.Windows.Visibility.Hidden;
            LoginGrid.IsEnabled = false;
            TopMostcheckBox.IsChecked = Properties.Settings.Default.TopMost;
            WidthSlider.Value = this.Width;
            HeightSlider.Value = this.Height;
        }

        private void ApplySettingsButton_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.PVPToolPath = pvpToolPathComboBox.Text;

            //TopMost = (bool) TopMostcheckBox.IsChecked; didn't work -.-
            if (TopMostcheckBox.IsChecked == true)
                Properties.Settings.Default.TopMost = true;
            else
                Properties.Settings.Default.TopMost = false;

            Properties.Settings.Default.WoWPath = WoWPathComboBox.Text;
            Properties.Settings.Default.Save();
            EditorTabControl.Visibility = System.Windows.Visibility.Visible;
            EditorTabControl.IsEnabled = true;
            SettingsGrid.Visibility = System.Windows.Visibility.Hidden;
            SettingsGrid.IsEnabled = false;
            this.Topmost = Properties.Settings.Default.TopMost;
            TopMostcheckBox2.IsChecked = TopMostcheckBox.IsChecked;
        }

        private void CancelSettingsButton_Click(object sender, RoutedEventArgs e)
        {
            EditorTabControl.Visibility = System.Windows.Visibility.Visible;
            EditorTabControl.IsEnabled = true;
            SettingsGrid.Visibility = System.Windows.Visibility.Hidden;
            SettingsGrid.IsEnabled = false;
        }

        private void Credits_Click(object sender, RoutedEventArgs e)
        {
            switch (Properties.Settings.Default.Language)
            {
                case "English":
                    MessageBox.Show("Lots of thanks to the project team. =P\nProject team members are listed below:\n\nLeader:\n      -Bapes\n      -doggy\n\nCoder:\n      -Jazerix", "Credits", MessageBoxButton.OK);
                    break;
                case "Deutsch":
                    MessageBox.Show("Vielen Dank an das Projektteam. =P\nMitglieder des Projektteams sind unten aufgelistet:\n\nLeiter:\n      -Bapes\n      -doggy\n\nCoder:\n      -Jazerix", "Credits", MessageBoxButton.OK);
                    break;
            }
        }

        private void Login_Click(object sender, RoutedEventArgs e)
        {
            LoginGrid.Visibility = System.Windows.Visibility.Visible;
            LoginGrid.IsEnabled = true;
            EditorTabControl.Visibility = System.Windows.Visibility.Hidden;
            EditorTabControl.IsEnabled = false;
            SettingsGrid.Visibility = System.Windows.Visibility.Hidden;
            SettingsGrid.IsEnabled = false;

            if (Properties.Settings.Default.RememberAccount)
            {
                RememberAccountcheckBox.IsChecked = true;
                AccounttextBox.Text = Properties.Settings.Default.Account;
            }
            RememberPasswordcheckBox.IsEnabled = Properties.Settings.Default.RememberAccount;
            if (Properties.Settings.Default.RememberPassword)
            {
                RememberPasswordcheckBox.IsChecked = true;
                for (int i = 0; i < PasswordBuffer.Length; i++)
                {
                    PasswordtextBox.Text += "*";
                }
            }
        }

        private void LoginCancelButton_Click(object sender, RoutedEventArgs e)
        {
            LoginGrid.Visibility = System.Windows.Visibility.Hidden;
            LoginGrid.IsEnabled = false;
            EditorTabControl.Visibility = System.Windows.Visibility.Visible;
            EditorTabControl.IsEnabled = true;
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)RememberAccountcheckBox.IsChecked)
            {
                Properties.Settings.Default.Account = AccounttextBox.Text;
                Properties.Settings.Default.RememberAccount = true;
            }
            else
            {
                Properties.Settings.Default.RememberAccount = false;
                Properties.Settings.Default.Account = "";
            }
            if ((bool)RememberPasswordcheckBox.IsChecked)
            {
                Properties.Settings.Default.RememberPassword = true;
                Properties.Settings.Default.Password = PasswordBuffer;
            }
            else
            {
                Properties.Settings.Default.RememberPassword = false;
                Properties.Settings.Default.Password = "";
            }
            Properties.Settings.Default.Save();

            AccounttextBox.Text = "";
            RememberAccountcheckBox.IsChecked = false;
            RememberPasswordcheckBox.IsChecked = false;

            LoginGrid.Visibility = System.Windows.Visibility.Hidden;
            LoginGrid.IsEnabled = false;
            EditorTabControl.Visibility = System.Windows.Visibility.Visible;
            EditorTabControl.IsEnabled = true;
        }

        private void PasswordtextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void RememberAccountcheckBox_Checked(object sender, RoutedEventArgs e)
        {
            RememberPasswordcheckBox.IsEnabled = (bool) RememberAccountcheckBox.IsChecked;
        }

        private void RememberAccountcheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            RememberPasswordcheckBox.IsEnabled = (bool)RememberAccountcheckBox.IsChecked;
        }

        private void Opacityslider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Opacity = Opacityslider.Value / 100;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void DragWindow(object sender, MouseButtonEventArgs args)
        {
            if (WindowState == System.Windows.WindowState.Maximized)
            {
                WindowState = System.Windows.WindowState.Normal;
            }
            else
            {
                DragMove();
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (Properties.Settings.Default.Maximized)
            {
                this.WindowState = System.Windows.WindowState.Normal;
            }
            else
            {
                this.WindowState = System.Windows.WindowState.Maximized;
            }
            Properties.Settings.Default.Maximized = !Properties.Settings.Default.Maximized;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        private void WidthSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.Width = WidthSlider.Value;
        }

        private void HeightSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.Height = HeightSlider.Value;
        }

        private void TopMostcheckBox2_Checked(object sender, RoutedEventArgs e)
        {
            this.Topmost = true;
            TopMostcheckBox.IsChecked = true;
        }

        private void TopMostcheckBox2_TouchUp(object sender, TouchEventArgs e)
        {

        }

        private void TopMostcheckBox2_Unchecked(object sender, RoutedEventArgs e)
        {
            TopMostcheckBox.IsChecked = false;
            this.Topmost = false;
        }

        private void LibrarytabItem_MouseEnter(object sender, MouseEventArgs e)
        {
            if (LibrarytabItem.IsFocused)
            {
                if (this.LoginStatus)
                {
                }
                else
                {
                    LoginGrid.Visibility = System.Windows.Visibility.Visible;
                    LoginGrid.IsEnabled = true;

                    EditorTabControl.Visibility = System.Windows.Visibility.Hidden;
                    EditorTabControl.IsEnabled = false;
                }
            }
        }

        private void LibrarytabItem_MouseLeave(object sender, MouseEventArgs e)
        {
            if (LibrarytabItem.IsFocused)
            {
                if (this.LoginStatus)
                {
                }
                else
                {
                    LoginGrid.Visibility = System.Windows.Visibility.Visible;
                    LoginGrid.IsEnabled = true;

                    EditorTabControl.Visibility = System.Windows.Visibility.Hidden;
                    EditorTabControl.IsEnabled = false;
                }
            }
        }

        private void Grid_MouseEnter(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.ScrollS;
        }

        private void Grid_MouseLeave(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Arrow;
        }
    }
}
